package com.fitsoftwareprocess.mobilemap.bus;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class MyWebClient {
	// private static final String SOAP_ACTION = "http://tempuri.org/GetMap";
	// private static final String METHOD_NAME = "GetMap";
	// private static final String NAMESPACE = "http://tempuri.org/";
	// private static final String URL = "http://10.0.2.2:1609/Service1.asmx";
	protected String _NameSpace;

	public MyWebClient(String nameSpace) {
		this._NameSpace = nameSpace;
	}

	public SoapObject GetSoapObject(String methodName) {
		return new SoapObject(_NameSpace, methodName);
	}

	public SoapSerializationEnvelope GetEnvelope(SoapObject Soap) {
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(Soap);
		return envelope;
	}

	public SoapPrimitive InvokeMethod(String URL, String methodName) {
		SoapObject request = GetSoapObject(methodName);
		SoapSerializationEnvelope envelope = GetEnvelope(request);
		return MakeCall(URL, envelope, _NameSpace, methodName);
	}

	public SoapPrimitive MakeCall(String URL, SoapSerializationEnvelope Envelope,
			String NAMESPACE, String METHOD_NAME) {
		HttpTransportSE httpTransport = new HttpTransportSE(URL);
		httpTransport.debug = true;
		try {
			httpTransport.call(NAMESPACE + METHOD_NAME, Envelope);
			SoapPrimitive response = (SoapPrimitive) Envelope.getResponse();
			return response;
		} catch (Exception e) {
			e.printStackTrace();

		}
		return null;
	}

	public String getNameSpace() {
		return this._NameSpace;
	}
}
